package com.student.dao.impl;
//@Classname SportingEventsDaoImpl
//@time 17:55
//OMEN

import com.student.dao.SportingEventsDao;
import com.student.entity.AdminInfo;
import com.student.entity.SportingEvents;
import com.student.util.DBUtil;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SportingEventsDaoImpl implements SportingEventsDao {
    @Override
    public int insert(SportingEvents entity) throws Exception {
        String sql="insert  into sporting_events(sporting_id,sporting_name,sporting_introduce)values (?,?,?)";
        try(
                Connection con= DBUtil.getConnection();
                PreparedStatement pstm=con.prepareStatement(sql);
        ) {
            pstm.setObject(1,entity.getSportingId());
            pstm.setObject(2,entity.getSporting_name());
            pstm.setObject(3,entity.getSporting_introduce());
            return pstm.executeUpdate();

        }
    }

    @Override
    public int updateById(SportingEvents entity) throws Exception {
        String sql="update sporting_events set sporting_name=?,sporting_introduce=? where sporting_id=?";
        try (
                Connection con=DBUtil.getConnection();
                PreparedStatement pstm=con.prepareStatement(sql);

        ){
            pstm.setObject(1,entity.getSporting_name());
            pstm.setObject(2,entity.getSporting_introduce());
            pstm.setObject(3,entity.getSportingId());
            return pstm.executeUpdate();


        }
    }

    @Override
    public int deleteById(Serializable id) throws Exception {
        String sql ="delete from sporting_events where sporting_id=?";
        try(
                Connection con=DBUtil.getConnection();
                PreparedStatement pstm=con.prepareStatement(sql);

        ) {
            pstm.setObject(1,id);
            return pstm.executeUpdate();
        }
    }

    @Override
    public SportingEvents selectById(Serializable id) throws SQLException {
        String sql ="select * from sporting_events where sporting_id=?";
        Connection con =null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        try {
            con=DBUtil.getConnection();
            pstm=con.prepareStatement(sql);
            pstm.setObject(1,id);
            rs=pstm.executeQuery();
            if (rs.next()){
                SportingEvents se=new SportingEvents();
                se.setSportingId(rs.getInt("sporting_id"));
                se.setSporting_name(rs.getString("sporting_name"));
                se.setSporting_introduce(rs.getString("sporting_introduce"));


                return se;

            }else {
                throw new SQLException("没有数据");
            }


        }finally {
            DBUtil.close(con,pstm,rs);

        }
    }

    @Override
    public List<SportingEvents> select(int index, int len) throws SQLException {
        String sql ="select * from sporting_events order by sporting_id limit ?,?";
        Connection con=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        try {
            con=DBUtil.getConnection();
            pstm=con.prepareStatement(sql);
            pstm.setObject(1,index);
            pstm.setObject(2,len);
            rs=pstm.executeQuery();
            List<SportingEvents> list=new ArrayList<>();
            while (rs.next()){
                SportingEvents se=new SportingEvents();
                se.setSportingId(rs.getInt("sporting_id"));
                se.setSporting_name(rs.getString("sporting_name"));
                se.setSporting_introduce(rs.getString("sporting_introduce"));
                list.add(se);
            }
            return list;

        }finally {
            DBUtil.close(con,pstm,rs);
        }
    }

    @Override
    public long count() throws SQLException {
        return 0;
    }
}
